import { Component, OnInit } from '@angular/core';
import { NavParams, NavController } from '@ionic/angular';
import { CacheService } from '@services/cache.service';
import { InstallService } from '@services/data.service/index';
import { MyNoteService } from '@services/myNote.service';
import { ActivatedRoute } from '@angular/router';
import { EmitService } from '@services/emit.service';


@Component({
    selector: 'installation',
    templateUrl: 'installation.html',
    styleUrls: ['installation.scss']
})

export class InstallationPage implements OnInit {
    // 传递页面信息参数
    roomInfo;
    defaultSet = [
        { imgSrc: 'assets/imgs/my/score/installation/step1-on.png', name: '设备识别', state: 0 },
        { imgSrc: 'assets/imgs/my/score/installation/step2-on.png', name: '安装固定', state: 1 },
        { imgSrc: 'assets/imgs/my/score/installation/step3-on.png', name: '接线检查', state: 1 },
        { imgSrc: 'assets/imgs/my/score/installation/step4-on.png', name: '通电激活', state: 1 },
        { imgSrc: 'assets/imgs/my/score/installation/step5-on.png', name: '初始设置', state: 1 },
        { imgSrc: 'assets/imgs/my/score/installation/step6-on.png', name: '数据校核', state: 1 },
        { imgSrc: 'assets/imgs/my/score/installation/step7-on.png', name: '完成', state: 1 }
    ];
    projectId;
    Step;
    installData = {};

    defaultExtend = {
        userId: this.cache.userinfo.userId,
        step: 0,                         // 当前进行到第几步
        state: 0,                        // 设备状态（0-未开始；1-进行中 ；2-已结束有问题；3-已结束无问题）
        startTime: null,                 // 开始时间
        Address: null,                      // 扫码得到的设备地址
        Code: 0,                         // 扫码得到的编号
        endTime: null,                   // 结束时间
        data1: null,                     // 扫描结果
        data2: null,                     // {a：安装示意图 ；b：拍照（图片路径+时间）；c:仪表-位置选择（例如：天花板）}
        data3: null,                     // {a：接线示意图 ； b：拍照（图片路径+时间） }
        data4: null,                     // {a:激活状态 ；b：激活次数 }（便于处于进行时设备的判断）
        data5: null,                     // 保存初始化的数据设置
        data6: null,                     // {a:采集参数列表 ; b:列表勾选情况; c : endTime }
        data7: null,
        areaId: null,
        meterId: null,
        mapping: null,                    // 安装位置
        meterLevel: null,                 // 选择的父仪表
        Relation: null                    // 参数选择
    };

    constructor(
        public navCtrl: NavController,
        public activatedRoute: ActivatedRoute,
        private cache: CacheService,
        private installServie: InstallService,
        private myNote: MyNoteService,
        private emitService: EmitService
    ) {
        this.projectId = this.cache.currentProject.projectId;
        this.roomInfo = this.emitService.installParams;
        console.log('初始安装房间信息', this.roomInfo);
        Object.assign(this.defaultExtend, this.roomInfo.device);

        if (this.roomInfo.installData) {
            // 存在后台存储则直接加载
            console.log('后台存储', this.roomInfo.installData);
            this.installData = this.roomInfo.installData;
        } else {
            // 不存在后台存储则取缓存
            this.installServie.getInstallData(this.projectId, this.defaultExtend.areaId, this.defaultExtend.meterId).subscribe(res => {
                console.log('非后台存储', res)
                if (!res) {
                    Object.assign(this.installData, this.defaultExtend);
                } else {
                    this.installData = res;
                }
                Object.assign(this.installData, this.roomInfo.device);
                this.installServie.saveInstallData(this.defaultExtend.areaId, this.defaultExtend.meterId, this.installData);
            });
        }
    }

    ngOnInit() {
        this.Step = {
            now: 0,
            state: 1
        };
    }

    NextNum(x) {
        for (let i = 0; i < this.defaultSet.length; i++) {
            if (i === x) {
                this.defaultSet[i].state = 0;
            }
        }
    }

    // 重新安装
    reChange() {
        console.log('重装', this.roomInfo);
        if (this.roomInfo.Type === 0) {
            this.installServie.clearStack(this.roomInfo.device.areaId, this.roomInfo.device.stackId, this.roomInfo.device.stackCode)
                .subscribe(res => {
                    if (res.success) {
                        this.clearStorage();
                        this.navCtrl.pop();
                    } else {
                        this.myNote.MyNotice(res.message);
                    }
                });
        } else {
            this.installServie.clearMeter(this.roomInfo.device.meterId).subscribe(res => {
                if (res.success) {
                    this.clearStorage();
                    this.navCtrl.navigateBack('tabs/my/InstrumentPage');
                } else {
                    this.myNote.MyNotice(res.message);
                }
            });
        }
    }

    goBack() {
        this.navCtrl.navigateBack('tabs/my/InstrumentPage');
    }

    // 清空本地缓存数据
    clearStorage() {
        this.installServie.saveInstallData(this.defaultExtend.areaId, this.defaultExtend.meterId, null);
    }
}

